﻿using LibNiconico.NiconicoApi.Extentions;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Alert
{
    /// <summary>
    /// アラート通知メッセージ
    /// </summary>
    public class NiconicoAlertNotificationMessage
    {
        private NiconicoAlertNotificationMessage()
        {

        }





        /// <summary>
        /// ID
        /// </summary>
        public string Id { get; private set; }

        /// <summary>
        /// コミュニティ番号またはチャンネルID
        /// </summary>
        public string CommunityOrChannnelId { get; private set; }


        /// <summary>
        /// 放送主のユーザID
        /// </summary>
        public string OwnerId { get; private set; }

        /// <summary>
        /// 公式か判定
        /// </summary>
        public bool IsOfficial
        {
            get
            {
                return this.CommunityOrChannnelId == "official";
            }
        }

        internal static NiconicoAlertNotificationMessage Parse(XElement element)
        {

            var message = new NiconicoAlertNotificationMessage();


            if (element != null)
            {


                var bodys = element.Value.Split(',');

                if (bodys.Length >= 3)
                {
                    message.Id = bodys[0];

                    long buffer;
                    if (long.TryParse(message.Id, out buffer))
                    {
                        message.Id = "lv" + message.Id;
                    }


                    message.CommunityOrChannnelId = bodys[1];
                    message.OwnerId = bodys[2];
                }
                else
                {
                    message.Id = "";
                    message.CommunityOrChannnelId = "";
                    message.OwnerId = "";
                }

            }

            return message;
        }


    }
}
